// pages/clubmanage/clubmanage.js
var util = require("../../utils/util.js")

const {
  $Message
} = require('../../iview/base/index');

Page({
  data: {
    tabarr: ["人员管理", "审核消息"],
    clubs: ["煎饼果子社", "麻辣香锅社"],
    TabCur: 0,
    scrollLeft: 0,
    tglleft: false,
    events: [],
    master:null,
    administrator1:null,
    administrator2:null,
    clubId:''
  },

  toggleLeft: function() {
    this.setData({
      tglleft: !this.data.tglleft
    })
  },

  changeClub:function(e){
    wx.redirectTo({
      url:'../../pages/clubmanage/clubmanage?id=' + e.currentTarget.id
    })
  },

  onChangeM: function (e) {
    // console.log(this.clubId)
    var modifyitem = this.data.clubId
    console.log(modifyitem)
    var itemcname = '社长openid'
    wx.navigateTo({
      url: '../modifysingleinfo/modifysingleinfo?modifyitem=' + modifyitem + '&itemcname=' + itemcname
    })
  },

  onChange1: function (e) {
    // console.log(this.clubId)
    var modifyitem = this.data.clubId
    var itemcname = 'openid'
    wx.navigateTo({
      url: '../modifysingleinfo/modifysingleinfo?modifyitem=' + modifyitem + '&itemcname=' + itemcname
    })
  },

  onChange2: function (e) {
    // console.log(this.clubId)
    var modifyitem = this.data.clubId
    console.log(modifyitem)
    var itemcname = '管理员2openid'
    wx.navigateTo({
      url: '../modifysingleinfo/modifysingleinfo?modifyitem=' + modifyitem + '&itemcname=' + itemcname
    })
  },

  onSetM: function (e) {
    // console.log(this.clubId)
    var modifyitem = this.data.clubId
    var itemcname = '社长openid'
    wx.navigateTo({
      url: '../modifysingleinfo/modifysingleinfo?modifyitem=' + modifyitem + '&itemcname=' + itemcname
    })
  },

  onSet: function (e) {
    // console.log(this.clubId)
    var modifyitem = this.data.clubId
    console.log(modifyitem)
    var itemcname = 'openid'
    wx.navigateTo({
      url: '../modifysingleinfo/modifysingleinfo?modifyitem=' + modifyitem + '&itemcname=' + itemcname
    })
  },
  
  onSet2: function (e) {
    // console.log(this.clubId)
    var modifyitem = this.data.clubId
    console.log(modifyitem)
    var itemcname = '管理员2openid'
    wx.navigateTo({
      url: '../modifysingleinfo/modifysingleinfo?modifyitem=' + modifyitem + '&itemcname=' + itemcname
    })
  },
  permit: function(e) {
    console.log(e)
    var that = this
    var date=new Date()
    const db = wx.cloud.database()
    db.collection("event").where({
      _id: e.currentTarget.id
    }).get().then(function(leres) {
      console.log(leres.data[0].legal)
      wx.cloud.callFunction({
        name: 'setEventLegal',
        data: {
          legal: !leres.data[0].legal,
          id: e.currentTarget.id,
          publishtime: date.getTime()
        }
      }).then(function(re) {
        db.collection("event").where({
          host: that.data.clubId
        }).get().then(function(res) {
          var temp =[]
          for (var i = 0; i < res.data.length; i++) {
            res.data[i].fmttimefrom = util.formatTime(res.data[i].timefrom)
            res.data[i].fmttimeto = util.formatTime(res.data[i].timeto)
            temp.push(res.data[i])
          //  if (date.getTime() > temp[i].timefrom.getTime()) {
        //      temp[i].show = false
         //   } else {
        //      temp[i].show = true
          //  }
          }
          that.setData({
            events: temp
          })
          $Message({
            content: '操作成功',
            type: 'success'
          })
        })
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var date = new Date()
    console.log(options)
    var that = this
    that.setData({
      clubId: options.id
    })
    wx.setNavigationBarTitle({
      title: '后台管理',
    })
    wx.cloud.init();
    const db = wx.cloud.database();
    const userco = db.collection("user")
    const clubco = db.collection("club")
    clubco.get().then(function(clubRes){
      that.setData({
        clubs:clubRes.data
      })
    })

    console.log('host为：'+options.id)


    db.collection("event").where({
      host: options.id    
    }).get().then(function(res) {
      var temp = that.data.events
      console.log('res data为：'+JSON.stringify(res.data))
      for (var i = 0; i < res.data.length; i++) {
        res.data[i].fmttimefrom = util.formatTime(res.data[i].timefrom)
        res.data[i].fmttimeto = util.formatTime(res.data[i].timeto)
        temp.push(res.data[i])
        if (date.getTime() > temp[i].timefrom.getTime()) {
          temp[i].show = false
        } else {
          temp[i].show = true
        }
      }
      console.log('temp为：'+JSON.stringify(temp))

      
      that.setData({
        events: temp
      },()=>{
        console.log('that data events 为：'+JSON.stringify(that.data.events))
      })
    })
    clubco.where({
      _id: this.data.clubId
    }).get().then(function(res) {
      // userco.where({
      //   _id: res.data[0].master
      // }).get().then(function(masterRes) {
      //   that.setData({
      //     master: masterRes.data[0]
      //   })
      // })
      console.log(res.data[0])
      if (res.data[0].master!=null){
        wx.cloud.callFunction({
          name: 'readuser',
          // 传给云函数的参数
          data: {
            _openid: res.data[0].master,
          },
          success(res) {
            console.log(res.result)
            that.setData({
              master: res.result.data[0]
            })
          },
          fail: console.error
        })
      }

      if (res.data[0].administrator1 != null) {
        console.log('anministrator1')
        wx.cloud.callFunction({
          name: 'readuser',
          // 传给云函数的参数
          data: {
            _openid: res.data[0].administrator1,
          },
          success(res) {
            console.log(res.result)
            that.setData({
              administrator1: res.result.data[0]
            })
          },
          fail: console.error
        })
      }

      if (res.data[0].administrator2 != null) {
        console.log('anministrator2')
        wx.cloud.callFunction({
          name: 'readuser',
          // 传给云函数的参数
          data: {
            _openid: res.data[0].administrator2,
          },
          success(res) {
            console.log(res.result)
            that.setData({
              administrator2: res.result.data[0]
            })
          },
          fail: console.error
        })
      }

      // userco.where({
      //   _id: res.data[0].administrator1
      // }).get().then(function(ad1Res) {
      //   that.setData({
      //     administrator1: ad1Res.data[0]
      //   })
      // })
      // userco.where({
      //   _id: res.data[0].administrator2
      // }).get().then(function(ad2Res) {
      //   that.setData({
      //     administrator2: ad2Res.data[0]
      //   })
      // })
    })


  },

  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id - 1) * 60
    })
  },

  gotoCreateClub:function(e){
    wx.navigateTo({
      url: '../../pages/createclub/createclub',
    })
  }
})
